package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.MemberRaiOrder;
import com.ruoyi.system.domain.SysComboMeal;
import com.ruoyi.system.domain.SysMember;
import com.ruoyi.system.domain.SysMemberRai;
import com.ruoyi.system.mapper.MemberRaiOrderMapper;
import com.ruoyi.system.mapper.SysComboMealMapper;
import com.ruoyi.system.mapper.SysMemberMapper;
import com.ruoyi.system.mapper.SysMemberRaiMapper;
import com.ruoyi.system.service.ISysMemberService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 会员信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-21
 */
@Service
public class SysMemberServiceImpl implements ISysMemberService 
{
    @Autowired
    private SysMemberMapper sysMemberMapper;
    @Autowired
    private SysMemberRaiMapper sysMemberRaiMapper;
    @Autowired
    private MemberRaiOrderMapper memberRaiOrderMapper;
    @Autowired
    private SysComboMealMapper sysComboMealMapper;

    /**
     * 查询会员信息
     * 
     * @param id 会员信息主键
     * @return 会员信息
     */
    @Override
    public SysMember selectSysMemberById(String id)
    {
        return sysMemberMapper.selectSysMemberById(id);
    }

    /**
     * 查询会员信息列表
     * 
     * @param sysMember 会员信息
     * @return 会员信息
     */
    @Override
    public List<SysMember> selectSysMemberList(SysMember sysMember)
    {
        List<SysComboMeal> sysComboMeals = sysComboMealMapper.selectSysComboMealList(new SysComboMeal());
        List<SysMember> sysMembers = sysMemberMapper.selectSysMemberList(sysMember);
        for (SysMember sysMember1 : sysMembers) {
            sysMember1.setPayProportion(ObjectUtils.isNotEmpty(sysComboMeals) ? sysComboMeals.get(0).getPayProportion() : null);
        }
        return sysMembers;
    }

    @Override
    public int insertSysMember(SysMember sysMember) {
        sysMember.setCreateTime(DateUtils.getNowDate());
        return sysMemberMapper.insertSysMember(sysMember);
    }

    /**
     * 新增会员信息
     * 
     * @param sysMember 会员信息
     * @return 结果
     */
    @Override
    public int insertSysMemberRtnId(SysMember sysMember)
    {
        sysMember.setCreateTime(DateUtils.getNowDate());
        return sysMemberMapper.insertSysMemberRtnId(sysMember);
    }

    /**
     * 修改会员信息
     * 
     * @param sysMember 会员信息
     * @return 结果
     */
    @Override
    public int updateSysMember(SysMember sysMember)
    {
        sysMember.setUpdateTime(DateUtils.getNowDate());
        return sysMemberMapper.updateSysMember(sysMember);
    }

    /**
     * 批量删除会员信息
     * 
     * @param ids 需要删除的会员信息主键
     * @return 结果
     */
    @Override
    public int deleteSysMemberByIds(String[] ids)
    {
        return sysMemberMapper.deleteSysMemberByIds(ids);
    }

    /**
     * 删除会员信息信息
     * 
     * @param id 会员信息主键
     * @return 结果
     */
    @Override
    public int deleteSysMemberById(String id)
    {
        return sysMemberMapper.deleteSysMemberById(id);
    }

    @Override
    public boolean checkPhoneByPhone(String phoneNumber) {
        return ObjectUtils.isNotEmpty(sysMemberMapper.getPhoneOnly(phoneNumber));
    }

    @Override
    public List<SysMemberRai> getMemberUseRai(String id) {
        List<SysMemberRai> sysMemberRais = sysMemberRaiMapper.selectSysMemberRaiList(new SysMemberRai());
        for (SysMemberRai sysMemberRai : sysMemberRais) {
                MemberRaiOrder param = new MemberRaiOrder();
                param.setMemberId(id);
                param.setRaiId(sysMemberRai.getId());
                List<MemberRaiOrder> memberRaiOrders = memberRaiOrderMapper.selectMemberRaiOrderList(param);
                if (!memberRaiOrders.isEmpty()){
                    // 已领取
                    sysMemberRai.setGetStatus("1");
                    // 已过期
                    Date createDate = memberRaiOrders.get(0).getCreateTime();
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(createDate);
                    calendar.add(Calendar.DATE, 30); // 添加30天
                    Date thirtyDaysLater = calendar.getTime();
                    Date today = new Date(); // 当前日期
                    // 或者如果要包含第30天当天
                    if (!today.before(thirtyDaysLater)){
                        sysMemberRai.setGetStatus("2");
                    }
                }else{
                    // 未领取
                    sysMemberRai.setGetStatus("0");
                }
        }
        return sysMemberRais;
    }

    @Override
    public List<SysMemberRai> getMemberUserRaiList(String id, String raiId) {
        SysMemberRai sysMemberRaiParam = new SysMemberRai();
        sysMemberRaiParam.setId(raiId);
        List<SysMemberRai> sysMemberRais = sysMemberRaiMapper.selectSysMemberRaiList(sysMemberRaiParam);
        for (SysMemberRai sysMemberRai : sysMemberRais) {
            MemberRaiOrder param = new MemberRaiOrder();
            param.setMemberId(id);
            param.setRaiId(sysMemberRai.getId());
            List<MemberRaiOrder> memberRaiOrders = memberRaiOrderMapper.selectMemberRaiOrderList(param);
            if (!memberRaiOrders.isEmpty()){
                // 已领取
                sysMemberRai.setGetStatus("1");
                // 已过期
                Date createDate = memberRaiOrders.get(0).getCreateTime();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(createDate);
                calendar.add(Calendar.DATE, 30); // 添加30天
                Date thirtyDaysLater = calendar.getTime();
                Date today = new Date(); // 当前日期
                // 或者如果要包含第30天当天
                if (!today.before(thirtyDaysLater)){
                    sysMemberRai.setGetStatus("2");
                }
            }else{
                // 未领取
                sysMemberRai.setGetStatus("0");
            }
        }
        return sysMemberRais;
    }
}
